﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Web;
using Api.Interface;
using BusinessLayer.RegInfo;
using BusinessLayer.RegInfo.Entities;
using CommandLib;
using Newtonsoft.Json;

namespace Api
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "FindArtInfoServices" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select FindArtInfoServices.svc or FindArtInfoServices.svc.cs at the Solution Explorer and start debugging.
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [ServiceContract]
    public class FindArtInfoServices : IFindArtInfoServices
    {
        private string PublicKey =
            "<RSAKeyValue><Modulus>rktMsiy2rx7mOKppJiinmTBEd0zS0pM1UuHjLmrF81rj0zohYKAQCnHT7NKxnVbELPgOe0nD6EQYc/lMU5AzrEh9Ru2rS6AawE8bBV89/JUO4SYrdtnM5CLqLdDE19e9sOdxqEQGQV1g1BlvAmnyVMo/PRWPlLqRJrGOCds6U7c=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
        [OperationContract]
        [WebGet(UriTemplate = "/DoWork?d={data}&s={sign}",
            BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
        //userName + "#" + password
        public void DoWork(string data, string sign)
        {
            //nghi
            string tg1 = Encrypt.EncodeTo64("nghiand#123");
            string tg = Encrypt.EncodeTo64(Encrypt.CreateSignRSA(tg1,
                                                "<RSAKeyValue><Modulus>rktMsiy2rx7mOKppJiinmTBEd0zS0pM1UuHjLmrF81rj0zohYKAQCnHT7NKxnVbELPgOe0nD6EQYc/lMU5AzrEh9Ru2rS6AawE8bBV89/JUO4SYrdtnM5CLqLdDE19e9sOdxqEQGQV1g1BlvAmnyVMo/PRWPlLqRJrGOCds6U7c=</Modulus><Exponent>AQAB</Exponent><P>86NNyFwDE4JClaupu9qUjPLXYiXunVr17W7W8XnYXoEjPCVFdDs722i7F64Zlru5JT+esFFx4O699yO4D9CEgQ==</P><Q>tyNEDZft5jEWq1LLRbNJw5VaW+6MNniuBMKUiHftIaLtLoZX0Xx+hueGDsZ5tlnWY5FmJ1Zb2QRwYdDvgA/cNw==</Q><DP>cvRYXPLdGLVbSVdHrLUDZkWmTk7reR9T7gVgMWt2JZD02QoU7TSNl4ag0Bl7RV2pi+OqsjKbcq/yvcsCq6ONAQ==</DP><DQ>g8x4hIPrrTd0xhWOvvm5w20wG2tBM06Ehl52wQP1/7gek9YRS20aLgJfgzCplWdwGDYi23qWHUfJjUvtoS9I3w==</DQ><InverseQ>7cFGjuduIkum8WUGYsyclWSz1s4cYcQuPFd7IpX4wOsveqIZclkkRFVYHOwKcw0jsUjOZaakOr6zB73yFOFeJg==</InverseQ><D>QIpJoEDGdRpwDdOslCDBzwv8jmC4QJmaSVK0GWAYZQDnFDSWX31l8zgrTarHcofKpQxF8IlkLgHpPCi4hskDGM7F9VEO6FZuO88R7LOEn/CoHxPgTvq3TXm9MNmN12Dw1mjdvPxmf71AaennqnV5muGbut/Rj/M+i9PjGG7mpAE=</D></RSAKeyValue>"));
            //end nghi
            //chungpk
            bool checkSign = Encrypt.CheckSignRSA(data, Encrypt.DecodeFrom64(sign), PublicKey);
            if(checkSign == true)
            {
                string[] arrayData = Encrypt.DecodeFrom64(data).Split('#');
                Account account = new Account();
                account = new AccountBL().GetByEmail(arrayData[0]);
                if(account != null)
                {
                    if(account.PassWord.Equals(arrayData[1]))
                    {
                        account.PassWord = "";
                        HttpContext.Current.Response.Redirect("http://localhost:6869/Artists/Result?artistName=" + Encrypt.EncodeTo64(JsonConvert.SerializeObject(account)));                        
                    }
                }
                //return JsonConvert.SerializeObject("false");                
            }
            //return JsonConvert.SerializeObject("false");
            //end chung pk
        }
    }
    
}
